package com.ehome.poct.backend.controller.device;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ehome.exception.BizException;
import com.ehome.oss.config.OssClient;
import com.ehome.oss.config.PutObjectProgressListener;
import com.ehome.oss.config.UploadInfo;
import com.ehome.poct.backend.api.VersonInfoDTO;
import com.ehome.poct.backend.util.EzipUtil;
import com.ehome.poct.base.api.constant.DeviceType;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;


@Slf4j
@Api(tags = "文件上传")
@ApiSupport(author = "yjm")
@RestController
@RequestMapping("/upload")
public class UploadController {
    
    @Autowired
    private OssClient ossClient;
    
    //@Autowired
    //private com.ehome.common.api.oss.OssClient ossClientc;
    
    @ApiOperation("文件上传之后返回相关数据")
    @ApiOperationSupport(author = "瑶苏", order = 1)
    @PostMapping("/file-upload/fileData")
    public VersonInfoDTO fileData(MultipartFile file) {
        VersonInfoDTO versonInfoDTO = EzipUtil.unzipJson(file);
        if (ObjectUtil.isEmpty(versonInfoDTO)) {
            throw new BizException("文件解析失败！");
        }
        List<VersonInfoDTO.minVersion> driverVersions = JSON.parseArray(versonInfoDTO.getMinDriverVersion(), VersonInfoDTO.minVersion.class);
        List<VersonInfoDTO.minVersion> softVersions = JSON.parseArray(versonInfoDTO.getMinSoftwareVersion(), VersonInfoDTO.minVersion.class);
        List<VersonInfoDTO.minVersion> firmVersions = JSON.parseArray(versonInfoDTO.getMinFirmwareVersion(), VersonInfoDTO.minVersion.class);
        var pname = versonInfoDTO.getProjectName().split(",");
        String type = "";
        for (String i : pname) {
            DeviceType deviceType = DeviceType.getDeviceTypeEnum(i);
            type += deviceType.getDeviceType().toString() + ",";
        }
        versonInfoDTO.setDeviceType(type.substring(0, type.length() - 1));
        File fileZip = EzipUtil.unzip(file);
        if (ObjectUtil.isEmpty(fileZip)) {
            throw new BizException("文件zip包解析失败！");
        }
        String url = ossClient.upload(versonInfoDTO.getVersion() + "-" + fileZip.getName(), FileUtil.getInputStream(fileZip));
        versonInfoDTO.setFileName(file.getOriginalFilename());
        versonInfoDTO.setSize(file.getSize());
        versonInfoDTO.setUrl(url);
        versonInfoDTO.setDriverVersions(driverVersions);
        versonInfoDTO.setSoftVersions(softVersions);
        versonInfoDTO.setDriverVersions(firmVersions);
        return versonInfoDTO;
    }
    
    @ApiOperation("文件上传进度条")
    @ApiOperationSupport(author = "瑶苏", order = 1)
    @PostMapping("/file")
    public UploadInfo fileData2(MultipartFile file) {
        UploadInfo uploadInfo = ossClient.uploadForProgress(file);
        return uploadInfo;
    }
    
    @ApiOperation("文件上传获取进度条")
    @ApiOperationSupport(author = "瑶苏", order = 1)
    @GetMapping("/progress")
    public Integer fileData3(@RequestParam String taskId) {
        PutObjectProgressListener progress = ossClient.getProgress(taskId);
        return progress.getPercent();
    }
    
    @ApiOperation("获取oss签名")
    @ApiOperationSupport(author = "瑶苏", order = 1)
    @GetMapping("/getOssSignature")
    public String  fileUpload()  {
        try {
            return ossClient.getOssSignature();
        }catch (UnsupportedEncodingException ex)
        {
            log.info("编码错误 "+ex.toString());
        }
        return StrUtil.EMPTY;
    }
}
